package cn.tedu.egaddress1.controller;

import cn.tedu.egaddress1.base.response.JsonResult;
import cn.tedu.egaddress1.base.response.StatusCode;
import cn.tedu.egaddress1.mapper.AddressMapper;
import cn.tedu.egaddress1.pojo.dto.AddressAddParam;
import cn.tedu.egaddress1.pojo.vo.AddressInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/address/")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("add")
    public JsonResult add(@RequestBody AddressAddParam addressAddParam){
        log.debug("addressAddParam = " + addressAddParam);
        int rows = addressMapper.insertAddress(addressAddParam);
        if (rows > 0){
            return JsonResult.ok();
        }

        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("list")
    public JsonResult list(){
        List<AddressInfoVO> addressInfoVOS = addressMapper.selectAddressList();

        return JsonResult.ok(addressInfoVOS);
    }

    @PostMapping("del")
    public JsonResult del(Integer id){
        log.debug("id = " + id);
        int rows = addressMapper.deleteById(id);

        if (rows > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}










